package cn.xd.base.controller.admin;

import cn.xd.base.application.dto.query.AdminMenuQuery;
import cn.xd.base.application.service.command.AdminMenuAppService;
import cn.xd.base.application.service.query.AdminMenuQueryAppService;
import cn.xd.base.domain.bean.command.AddAdminMenuCommand;
import cn.xd.base.domain.bean.command.UpdateAdminMenuCommand;
import cn.xd.base.domain.bean.response.AdminMenuDTO;
import cn.xd.base.domain.bean.response.JsonAdminMenuTree;
import cn.xd.common.response.ResponseResult;
import cn.xd.web.utils.SecurityUtils;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 菜单管理（adminMenu）
 *
 * @author mirzalin
 */
@RestController
@RequestMapping("/admin/menu")
public class AdminMenuController {
    @Resource
    private AdminMenuAppService adminMenuAppService;

    @Resource
    private AdminMenuQueryAppService adminMenuQueryAppService;

    /**
     * 新增
     *
     * @param command
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<AdminMenuDTO> add(@Valid @RequestBody AddAdminMenuCommand command) {
        return ResponseResult.success(adminMenuAppService.addAdminMenu(command, SecurityUtils.getUserId()));
    }

    /**
     * 更新
     *
     * @param command
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<AdminMenuDTO> update(@Valid @RequestBody UpdateAdminMenuCommand command) {
        return ResponseResult.success(adminMenuAppService.updateAdminMenu(command, SecurityUtils.getUserId()));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("/delete/{id}")
    public ResponseResult<?> delete(@PathVariable Long id) {
        adminMenuAppService.deleteAdminMenu(id);
        return ResponseResult.success();
    }


    /**
     * 查询菜单（不分页）
     *
     * @param query
     * @return
     */
    @PostMapping("/list")
    public ResponseResult<List<AdminMenuDTO>> getList(@RequestBody AdminMenuQuery query) {
        return ResponseResult.success(adminMenuQueryAppService.getAdminMenuList(query));
    }

    /**
     * 查询菜单（tree）
     *
     * @param query
     * @return
     */
    @PostMapping("/getAdminMenuJsonTree")
    public ResponseResult<List<JsonAdminMenuTree>> getAdminMenuJsonTree(@RequestBody AdminMenuQuery query) {
        return ResponseResult.success(adminMenuQueryAppService.getAdminMenuJsonTree(query.getParentId(), new ArrayList<>()));
    }
}
